package com.example.homework

import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.style.BackgroundColorSpan
import android.view.View
import androidx.core.view.isVisible
import com.example.homework.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
    var counter: Int = 0
    lateinit var binding: ActivityMainBinding
    var text: String = "Bus is free"
    var color: Int = Color.GREEN
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        binding.incrementButton.setOnClickListener {
            inc()
            setText()
        }
        binding.decrementButton.setOnClickListener {
            desc()
            setText()
        }
        binding.resetButton.setOnClickListener {
            reset()
            setText()
        }
        setText()
    }

    fun setText() {
        binding.alertTextView.text = text
        binding.alertTextView.setTextColor(color)
        binding.counterTextView.text = counter.toString()
    }

    fun inc() {
        if (counter < 50) {
            counter++;
            text = (50 - counter).toString() + " seats left"
            color = Color.BLACK
            binding.decrementButton.isEnabled = true
        }
        if (counter == 50) {
            text = "Bus is full"
            color = Color.RED
            binding.resetButton.visibility = View.VISIBLE
            binding.incrementButton.isEnabled = false
        }
    }

    fun desc() {
        if (counter > 0) {
            counter--;
            text = (50 - counter).toString() + " seats left"
            color = Color.BLACK
            binding.incrementButton.isEnabled = true
            binding.resetButton.visibility = View.INVISIBLE
        }
        if (counter == 0) {
            text = "Bus is free"
            color = Color.GREEN
            binding.decrementButton.isEnabled = false
        }
    }

    fun reset() {
        binding.decrementButton.isEnabled = false
        binding.incrementButton.isEnabled = true
        counter = 0
        desc()
        binding.alertTextView.setTextColor(Color.GREEN)
        binding.resetButton.visibility = View.INVISIBLE
        binding.counterTextView.text = counter.toString()
    }
}